<?php
$title = '寻物启事提交';
include 'commonHeader.php';
?>
<body>
<div class="entry entry-top">
<?php

$success = 0;
$filesuccess = 1;

if (empty($_POST['kind']) || empty($_POST['position']) || empty($_POST['date']) || empty($_POST['name']) || empty($_POST['found_phone']) || empty($_POST['remark'])) {
	echo "提交失败！填写信息不完整！";
}
else {
	$filename = '';
	if(is_uploaded_file($_FILES["file"]['tmp_name'])){
	$domain = "picture";
	$upload_dir = "saestor://" . $domain . "/lostandfound/";

	if (!is_dir($upload_dir)) {
		//mkdir($upload_dir , 0777);
	}

	if ($_FILES["file"]["error"] > 0) {
		echo "文件上传失败！<br>请向管理员提交以下错误信息：" . $_FILES["file"]["error"] . "<br>" ;
		$filesuccess = 0;
	}/*
	else {

		echo "Upload: " . $_FILES["file"]["name"] . "<br>";
		echo "Type: " . $_FILES["file"]["type"] . "<br>";
		echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>";
		echo "Stored in: " . $_FILES["file"]["tmp_name"];

	}*/
	elseif (!in_array($_FILES["file"]["type"], array('image/jpeg', 'image/gif', 'image/png', 'image/x-png', 'image/pjpeg'))) {
		echo "文件上传失败！文件类型不正确！<br>";
		$filesuccess = 0;

	}
	else {
		$filename = md5(time() . lcg_value()) . '.' . addslashes(htmlspecialchars(pathinfo($_FILES["file"]['name'], PATHINFO_EXTENSION))); // 以当前时间与一个伪随机数的 MD5 值为文件名，减少文件重名冲突
		/*if (move_uploaded_file($_FILES["file"]["tmp_name"], $upload_dir . $filename)) {
			$add = $filename; // 'http://whdxxshxcb-picture.stor.sinaapp.com/lostandfound/' . $_FILES["file"]['name'];
		}*/
		if (!move_uploaded_file($_FILES["file"]["tmp_name"], $upload_dir . $filename)) {
			echo "上传文件失败！<br>请向管理员提交以下错误信息：" . $_FILES["file"]["error"] . "<br>";
			$filename = '';//转移失败重置文件名
			$filesuccess = 0;
		}
		}
	}
	if ($filesuccess == 0) $success = 0;
	else {
		$kind = addslashes(htmlspecialchars($_POST['kind']));
		$position = addslashes(htmlspecialchars($_POST['position']));
		$date = addslashes(htmlspecialchars($_POST['date']));
		$name = addslashes(htmlspecialchars($_POST['name']));
		$found_phone = addslashes(htmlspecialchars($_POST['found_phone']));
		$school = addslashes(htmlspecialchars($_POST['school']));
		$remark = addslashes(htmlspecialchars($_POST['remark']));

		// 连主库
		$link = mysql_connect(SAE_MYSQL_HOST_M . ':' . SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS);

		//$link = mysql_connect('127.0.0.1:3306', 'root', '');

		// 连从库
		// $link=mysql_connect(SAE_MYSQL_HOST_S.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);

		if ($link) {
			mysql_select_db(SAE_MYSQL_DB, $link);
			mysql_query("set names utf8");
		//	mysql_select_db('lostandfound', $link);

			//your code goes here
			$no = mysql_query("select max(no) from lostandfound");
			//echo $no;
			$noo = mysql_fetch_array($no);
			//echo $noo['no'];
			$sql = "INSERT INTO lostandfound (pic, lorf, kind, position, date, name, found_phone, school, remark)
				VALUES
				('$filename', 'l', '$kind', '$position', '$date', '$name', '$found_phone', '$school', '$remark')";

			if (!mysql_query($sql, $link)) {
				echo '提交失败！<br>请向管理员提交以下错误信息：' . mysql_error();
			}
			else {
				echo '提交成功！';
				$success = 1;
			}
			// echo "=<div class='ss'><a class='s'>提交成功!</a></div>";
			mysql_close($link);
		}
	}
}
?>
</div>
<div class="entry">
	<a href="lostpublish.php">
		<input type="button" class="button button-1" value="查看寻物启事">
	</a>
	<?php 
	if ($success == 0) {
	?>
	<a href="javascript: history.go(-1)">
		<input type="button" class="button button-2" value="返回重新填写">
	</a>
	<?php
	}
	?>
</div>
<div class="footer footer-bottom">Copyright © 2015 校会快帮</div>
</body>
</html>
